#include "mainwindow.h"
#include <QDockWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSlider>
#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // --- 中央 OpenGL Widget ---
    m_glWidget = new OpenGLWidget(this);
    setCentralWidget(m_glWidget);

    // --- 右侧面板 ---
    QWidget *rightPanel = new QWidget(this);
    QVBoxLayout *panelLayout = new QVBoxLayout(rightPanel);

    // 旋转控制
    QLabel *rotLabel = new QLabel("Rotation Angle:");
    m_rotationSlider = new QSlider(Qt::Horizontal);
    m_rotationSlider->setRange(0, 360);
    connect(m_rotationSlider, &QSlider::valueChanged, [this](int value) {
        m_glWidget->setRotationAngle(value);
    });

    // 颜色控制
    QLabel *colorLabel = new QLabel("Color Factor:");
    m_colorSlider = new QSlider(Qt::Horizontal);
    m_colorSlider->setRange(0, 100);
    m_colorSlider->setValue(50);
    connect(m_colorSlider, &QSlider::valueChanged, [this](int value) {
        m_glWidget->setColorFactor(value / 100.0f);
    });

    // 显示开关
    m_triCheckBox = new QCheckBox("Show Triangle");
    m_triCheckBox->setChecked(true);
    connect(m_triCheckBox, &QCheckBox::toggled, m_glWidget, &OpenGLWidget::setShowTriangle);

    m_squareCheckBox = new QCheckBox("Show Square");
    m_squareCheckBox->setChecked(true);
    connect(m_squareCheckBox, &QCheckBox::toggled, m_glWidget, &OpenGLWidget::setShowSquare);

    // 添加到面板
    panelLayout->addWidget(rotLabel);
    panelLayout->addWidget(m_rotationSlider);
    panelLayout->addWidget(colorLabel);
    panelLayout->addWidget(m_colorSlider);
    panelLayout->addWidget(m_triCheckBox);
    panelLayout->addWidget(m_squareCheckBox);
    panelLayout->addStretch();

    // --- 停靠窗口 ---
    QDockWidget *dock = new QDockWidget("Control Panel", this);
    dock->setWidget(rightPanel);
    addDockWidget(Qt::RightDockWidgetArea, dock);

    // --- 菜单栏 ---
    QMenu *fileMenu = menuBar()->addMenu("File");
    fileMenu->addAction("Exit", this, &QWidget::close);

    QMenu *viewMenu = menuBar()->addMenu("View");
    viewMenu->addAction("Reset Camera", [this] {
        m_glWidget->setFocus();
        // 可在此重置 cameraPos
    });

    // --- 工具栏 ---
    QToolBar *toolBar = addToolBar("Main");
    toolBar->addAction("Reset Rotation", [this] {
        m_rotationSlider->setValue(0);
    });

    // --- 状态栏 ---
    statusBar()->showMessage("Ready");

    // 设置窗口标题
    setWindowTitle("Qt + OpenGL - Day 5 Project");
    resize(1000, 600);
}

MainWindow::~MainWindow() = default;